
var fs = require('fs');
var path = require('path')

function writeFile(file,stringData) {
	return $.Deferred(function(d){
		fs.writeFile(file, stringData, function (err) {
			if (err){
				d.reject(err);
			} else {
				d.resolve();
			}
		});
	});
}

function writeJsonFile(file,obj){
	return $.Deferred(function(d){
		var stringData = obj ? JSON.stringify(obj) : '';
		fs.writeFile(file, stringData, function (err) {
			if (err){
				d.reject(err);
			} else {
				d.resolve();
			}
		});
	});
}
function writeJsonFileSync(file,obj){
	var stringData = obj ? JSON.stringify(obj) : '';
    fs.writeFileSync(file, stringData);

}
function writeFileSync(file,stringData){
	return fs.writeFileSync(file, stringData,'utf8');
}


function readFile(file) {
	return $.Deferred(function(d){
		fs.readFile(file,'utf8', function (err, stringData) {
			if (err){
				d.reject(err);
			} else {
				d.resolve(stringData);
			}
		});
	});
}


function readJsonFile(file) {
	return $.Deferred(function(d){
		fs.access(file, fs.F_OK, function(err){
			if(err){
				d.resolve(null,'');
			} else {
				fs.readFile(file,'utf8', function (err, stringData) {
					if (err){
						d.reject(err);
					} else {
                        var obj;
                        try{
                            obj = JSON.parse(stringData);
                        } catch(e){

                        }
						d.resolve(obj,stringData);
					}
				});
			}
		});
	});
}

function readFileSync(file) {
	var existsSync = fs.existsSync(file);
	if(existsSync){
		return fs.readFileSync(file,'utf8');
	} else {
		return null;
	}
}
function readJsonFileSync(file) {
	var existsSync = fs.existsSync(file);
	if(existsSync){
		var jsonString = fs.readFileSync(file,'utf8');
		var obj;
		try{
		    obj = JSON.parse(jsonString.toString());
        } catch(e){

        }
		return obj;
	} else {
		return null;
	}
}


// do_not_recursive : 设置为不遍历深层次文件夹
function getAllFilepathInDirSync(folder_path,filter,do_not_recursive) {
	folder_path = path.normalize(folder_path);
	let all_files = [];
	const files = fs.readdirSync(folder_path,{encoding:'utf8'});
	if( (!files) && (!files.length) && (!files.push) ){
		return all_files;
	}
	const len = files.length;
	for(let i=0; i<len; i++){
		const file = files[i];
		if((file==='.') || (file==='..')){
			continue;
		}
		const fullpath = path.join(folder_path,file);
		const stat = fs.lstatSync(fullpath);

		// 判断是否合法的stat对象
		if((!stat) || (!stat.isFile) || (!stat.isDirectory)){
			continue;
		}
		if(stat.isFile()){
			if(typeof filter==='function'){
				if(filter(fullpath,stat,file)){
					all_files.push(fullpath);
				}
			} else {
				all_files.push(fullpath);
			}
		} else if(stat.isDirectory()){
			if(do_not_recursive){
				continue;
			} else {
				const the_sub_dir_files = getAllFilepathInDirSync(fullpath,filter);
				all_files = all_files.concat(the_sub_dir_files);
			}
		} else {
			continue;
		}
	}

	return all_files;

}

var file = {
	writeFile:writeFile,
	writeFileSync:writeFileSync,
	writeJsonFileSync:writeJsonFileSync,
    readJsonFileSync:readJsonFileSync,
    readFileSync:readFileSync,

	getAllFilepathInDirSync:getAllFilepathInDirSync
};

module.exports=file;